package lotteryGui;


import java.awt.Font;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import lotteryModel.Users;



@SuppressWarnings("serial")
public class Logining extends JFrame {
    private TextField textField1;
    private TextField textField2;
    private String username1;
    private String password1;
    private Users user1;
    public Logining() {
        initComponents();
    }

    private void initComponents() {

        Label username = new Label();
        Label password = new Label();
        Label text = new Label();
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        textField1 = new TextField();
        textField2 = new TextField();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        text.setFont(new Font("Dialog", 0, 28));
        text.setText("JIT Login");

        username.setFont(new Font("Dialog", 0, 24));
        username.setText("username");

        password.setFont(new Font("Dialog", 0, 24));
        password.setText("password");


        jButton1.setText("登入");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("注册");
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(33, 33, 33)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(text, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                                .addGap(28, 28, 28)

                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(121, 121, 121)
                                                .addComponent(jButton1)
                                                .addGap(57, 57, 57)
                                                .addComponent(jButton2)))
                                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(73, 73, 73)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(text, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(username, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))

                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton1)
                                        .addComponent(jButton2))
                                .addGap(43, 43, 43))
        );

        pack();
    }


    private void jButton1ActionPerformed(ActionEvent evt) {
        // TODO add your handling code here:
        FileReader fr = null;
        username1 = textField2.getText();
        password1 = textField1.getText();
        //Users user1 = null;
        try {
            String filePath = System.getProperty("user.dir");  //获得当前程序路径
            String fileName = filePath+"/Users.txt";
            fr = new FileReader(fileName);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(compare(fr,username1,password1)) {

            setVisible(false);
            new Interphase(user1);

        }
        else if(username1.equals("1")&&password1.equals("1")) {
            setVisible(false);
            new Manager();
        }
        else {
            JOptionPane.showMessageDialog(null, "请重新输入", "用户名密码错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private boolean compare(FileReader fr,String name,String password) {
        BufferedReader bf = new BufferedReader(fr);
        String str;
        try {
            while((str = bf.readLine())!=null) {
                String[] strs = str.split(" ");
                user1 = new Users(strs[0],strs[1],strs[2],Integer.parseInt(strs[3]),strs[4]);
                if(strs[1].equals(name)&&strs[2].equals(password)) return true;
            }
        }
        catch(IOException e){
            System.out.println(e);
        }
        return false;
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        // TODO add your handling code here:
        setVisible(false);
        new Register().setVisible(true);
    }
    public static void main(String args[]) {

    }
}
